package com.csw.encryptiontechnology.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import sun.misc.BASE64Decoder;

/**
 * 加密工具类 实现aes加密、解密
 */
@SuppressWarnings("restriction")
public class AesUtil {

	// 密钥（16进制，和前台保持一致，或者是作为参数直接传过来也可以）
	private static final String DEFAULT_KEY = "1111wwww2222uuuu";
	// 算法PKCS5Padding
	private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
	// 测试数据
	private static final String TEST_CONTENT = "1";

	// 自测
	public static void main(String[] args) throws Exception {
		System.out.println("加密前：" + TEST_CONTENT);

		String encrypt = encrypt(TEST_CONTENT);
		System.out.println("加密后：" + encrypt);

		String decrypt = decrypt(encrypt);
		System.out.println("解密后：" + decrypt);
	}

	// AES加密--为base 64 code
	public static String encrypt(String content) throws Exception {
		return base64Encode(aesEncryptToBytes(content));
	}

	// AES解密--并解密base 64 code
	public static String decrypt(String encryptStr) throws Exception {
		//encryptStr.replaceAll("#", "/");
		return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr));
	}

	// base 64 encode编码
	public static String base64Encode(byte[] bytes) {
		return Base64.encodeBase64String(bytes);
	}

	// base 64 decode解码---》因为传过来的值是通过base64编码而后再进行aes加密出来的，所以解密之前先进行base64解码
	public static byte[] base64Decode(String base64Code) throws Exception {
		return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
	}

	// AES加密
	public static byte[] aesEncryptToBytes(String content) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(DEFAULT_KEY.getBytes(), "AES"));

		return cipher.doFinal(content.getBytes("utf-8"));
	}

	// AES解密
	public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
		KeyGenerator kgen = KeyGenerator.getInstance("AES");
		kgen.init(128);
		Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(DEFAULT_KEY.getBytes(), "AES"));
		byte[] decryptBytes = cipher.doFinal(encryptBytes);

		return new String(decryptBytes, "utf-8");
	}

}
